<!--  -->
<template>
  <div class="admin">
    <div class="screen">
      <div class="screen-top">
        <div class="span">
          <el-icon><Search /></el-icon>
          <span>筛选搜索</span>
        </div>
        <div>
          <el-button>重置</el-button>
          <el-button type="primary">查询搜索</el-button>
        </div>
      </div>
      <div class="screen-bottom">
        <el-form-item label="账号/姓名:">
          <el-input v-model="input" placeholder="请输入账号、姓名" />
        </el-form-item>
      </div>
    </div>
    <div class="dataGrid">
      <div class="span">
        <el-icon><Tickets /></el-icon>
        <span>数据列表</span>
      </div>
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="nickName" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column label="创建时间">
          <template #default="{ row }">{{
            formatDate(row.createTime)
          }}</template>
        </el-table-column>
        <el-table-column label="最后登录"
          ><template #default="{ row }">{{
            formatDate(row.loginTime)
          }}</template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template #default="{ row }">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="row.status"
          /></template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" @click="allocRoleInfo(row)"
              >分配角色</el-button
            >
            <el-button type="primary" @click="changeIsShow(row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="paging">
      <div class="demo-pagination-block">
        <el-pagination
          v-model:currentPage="pager.currentPage"
          v-model:page-size="pager.pageSize"
          :page-sizes="[5, 10, 20, 50, 100]"
          :small="pager.small"
          :disabled="pager.disabled"
          :background="pager.background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pager.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
  <!-- 用户编辑添加弹框 -->
  <Dialog :isShow="isShow" @close="falseIsShow" :form="form" />
  <!-- 分配角色 -->
  <AllocRoles
    :visible="allocRolesVisible"
    @close="closeAllocRole"
    :admin-id="adminId"
  />
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { umsAdmin } from "../../../request/http";
import dayjs from "dayjs";
import Dialog from "./components/Dialog.vue";
import AllocRoles from "./components/AllocRoles.vue";

const tableData = ref([] as {}[]);
const pager = reactive({
  currentPage: 1,
  pageSize: 10,
  small: true,
  disabled: false,
  background: false,
  total: 0,
});

// 分配角色弹框操作
const allocRolesVisible = ref(false);
const adminId = ref();
function allocRoleInfo(row: AdminResDataObj) {
  allocRolesVisible.value = true;
  adminId.value = row.id;
}
// 关闭用户分配角色弹框
function closeAllocRole() {
  allocRolesVisible.value = false;
}

const input = ref("");

const isShow = ref(false); // 控制编辑添加对话框显示隐藏

const form = ref({} as AdminResDataObj); // 控制编辑添加对话框内的数据

function handleSizeChange() {
  fetchData(pager.currentPage, pager.pageSize);
}
function handleCurrentChange() {
  fetchData(pager.currentPage, pager.pageSize);
}

async function fetchData(pageNum: number = 1, pageSize: number = 10) {
  const adminListRes = await umsAdmin.getAdminList({
    pageNum,
    pageSize,
  });
  if (adminListRes.code === 200) {
    tableData.value = adminListRes.data.list;
    pager.total = adminListRes.data.total;
  }
}
// 初步页面加载数据
fetchData(pager.currentPage, pager.pageSize);
//格式化时间
function formatDate(time?: string) {
  if (!time) return "";
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
}

// 点击编辑显示隐藏对话框
function changeIsShow(row: AdminResDataObj) {
  isShow.value = true;
  form.value = row;
}
function falseIsShow(r?: "reload") {
  isShow.value = false;
  if (r === "reload") {
    fetchData();
  }
}
// 点击添加
function add() {
  isShow.value = true;
  form.value = {} as AdminResDataObj;
}
</script>

<style lang="less" scoped>
.admin {
  overflow: auto;
  padding: 15px;
  box-sizing: border-box;
  .screen {
    height: 130px;
    border: 1px solid #eee;
    padding: 20px;
    box-sizing: border-box;
    .screen-top {
      display: flex;
      justify-content: space-between;
      .span {
        span {
          margin-left: 10px;
        }
      }
    }
    .screen-bottom {
      padding: 15px 30px;
      box-sizing: border-box;
      width: 350px;
    }
  }
  .dataGrid {
    height: 70px;
    border: 1px solid #eee;
    margin: 20px 0;
    align-items: center;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    .span {
      span {
        margin-left: 15px;
      }
    }
  }
  .paging {
    display: flex;
    justify-content: end;
  }
}
</style>
